package com.xujl.mvcbind.db;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.xujl.fastlib.utils.LogS;
import com.xujl.mvcbind.db.dao.DaoMaster;
import com.xujl.mvcbind.db.dao.DaoSession;

/**
 * 数据库管理器
 */
public class DBManager {
    private static final String TAG = "DBManager";
    public static final String DB_NAME = "MvcBind.db";
    private DaoSession daoSession;
    private OverTimeHelper mOverTimeHelper;

    private DBManager () {
    }

    public void initGreenDao (Application application) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(new GreenDaoContext(application), DB_NAME);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        LogS.d(TAG, "数据库初始化完成");
    }


    public synchronized OverTimeHelper getOverTimeHelper () {
        if (mOverTimeHelper == null) {
            mOverTimeHelper = new OverTimeHelper(daoSession);
        }
        return mOverTimeHelper;
    }

    public DaoSession getDaoSession () {
        return daoSession;
    }


    public static DBManager getInstance () {
        return Holder.MANAGER;
    }

    private static class Holder {
        private static final DBManager MANAGER = new DBManager();
    }
}
